package com.dundun.SSO.controller.manage;

import com.dundun.SSO.core.base.BaseController;
import com.dundun.SSO.manage.ISsoUserService;
import com.dundun.SSO.manage.pojo.SsoUser;
import com.dundun.SSO.manage.query.SsoUserQuery;
import com.dundun.SSO.pojo.ResponseBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/5/23 0023 17:55
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
@Controller
@RequestMapping("/sso/user")
public class SsoUserController extends BaseController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISsoUserService ssoUserService;

    public String getPage(){
        return "ssoUser";
    }

    @RequestMapping("/qryList.json")
    @ResponseBody
    public ResponseBean getSsoUserList(@RequestBody SsoUserQuery ssoUserQuery){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<SsoUser> ssoUserList = ssoUserService.selectByPage(ssoUserQuery);
            int count = ssoUserService.selectCountByPage(ssoUserQuery);
            responseBean.setRows(ssoUserList);
            responseBean.setTotal(count);
        }catch (Exception e){
            logger.error("getSsoUserList",e);
        }finally {
            return responseBean;
        }
    }

    @RequestMapping("update.json")
    @ResponseBody
    public ResponseBean updateSsoUser(@RequestBody SsoUser ssoUser){
        ResponseBean responseBean = new ResponseBean();
        try{
            if(ssoUser.getId() == null){
                return ResponseBean.parameterError("id不能为空");
            }
            if(StringUtils.isBlank(ssoUser.getName())){
                return ResponseBean.parameterError("名称不能为空");
            }
            ssoUser.setModifyTime(new Date());
            if(ssoUserService.updateByPrimaryKey(ssoUser)>0){
                return ResponseBean.success(null,null);
            }else{
                return ResponseBean.unknowError(null,null);
            }
        }catch (Exception e){
            logger.error("updateSsoUser",e);
        }
        return responseBean;
    }

    @RequestMapping("add.json")
    @ResponseBody
    public ResponseBean addSsoUser(@RequestBody SsoUser ssoUser){
        ResponseBean responseBean = new ResponseBean();
        try{
            if(StringUtils.isBlank(ssoUser.getAccount())){
                return ResponseBean.parameterError("账号不能为空");
            }
            if(StringUtils.isBlank(ssoUser.getName())){
                return ResponseBean.parameterError("名称不能为空");
            }
            ssoUserService.saveNotNull(ssoUser);
        }catch (Exception e){

        }
        return responseBean;
    }

    @RequestMapping("delete.json")
    @ResponseBody
    public ResponseBean deleteSsoUser(@RequestBody Long[] ids){
        ResponseBean responseBean = new ResponseBean();
        return ResponseBean.success("",null);
    }
}
